package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.MenuDao;
import com.sp.scmdb.dao.UserDao;
import com.sp.scmdb.entity.Menu;
import com.sp.scmdb.entity.Role;
import com.sp.scmdb.entity.User;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	/**用户接口*/
	private UserDao userDao;
	
	private MenuDao menuDao;
	
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	/**日志service*/
	private LogService logs;
	
	/**
	 * @param logs the logs to set
	 */
	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}

	/**
	 * @param userDao
	 * the userDao to set
	 */
	@Required
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public User login(UserBean userBean) {
		StringBuffer Hql = new StringBuffer("from User where 1=1 ");
		User user = null;
		if (StringUtils.isNotEmpty(userBean.getUserName())) {
			Hql.append("and userName='").append(userBean.getUserName().trim()).append("'");
			Hql.append("and passWord='").append(userBean.getPassWord().trim()).append("'");
			List<User> list = userDao.queryHql(Hql.toString());
			if (!CollectionUtils.isEmpty(list)) {
				user = (User) list.get(0);
				userBean.setOldUserName(user.getUserName());
				//logs.addLog(userBean, user.getUserName()+" / 登陆成功！");
				return user;
			}
		}
		return user;
	}
	/**添加用户*/
	@Transactional
	public String newUser(UserBean t) {
		String result = "3";
		String userName = t.getUserName().trim();
		StringBuffer Hql = new StringBuffer("from User where 1=1 ");
		if (StringUtils.isNotEmpty(t.getUserName())) {
			Hql.append("and userName='").append(userName).append("'");
			List<User> list = userDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				User user = new User();
				user.setIdRole(t.getIdRole());
				user.setUserName(userName);
				user.setPassWord(t.getPassWord().trim());
				user.setRmk(t.getRmk());
				userDao.save(user);
				/**添加日志*/
				logs.addLog(t, "添加 用户："+user.getUserName()+" 成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}
	/**删除用户*/
	@Transactional
	public String delUser(String[] arr,UserBean t) {
		User user = new User();
		String result = "3";
		if (arr.length > 0) {
			for (int i = 0; i < arr.length; i++) {
				user = this.findUser(arr[i] + "").get(0);
				
				//rmk为1的数据时不可以删除的,系统保留用户
				if("1".equals(user.getRmk())){
					return CommonParamter.CHECK_SYSTEMRESERVED;
				}else{
					userDao.delete(user);
				}

				/**添加日志*/
				logs.addLog(t,"删除 用户： "+ user.getUserName()+" 成功！");
			}
			result = CommonParamter.CHECK_SUCCESS;
		} else {
			result = CommonParamter.CHECK_FAIL;
		}
		return result;
	}
	/**编辑用户*/
	@Transactional
	public String editUser(UserBean t) {

		String result = "3";
		String userName = t.getUserName().trim();
		StringBuffer Hql = new StringBuffer("from User where 1=1");
		if (StringUtils.isNotEmpty(t.getUserName())) {
			Hql.append(" and userName='").append(userName).append("' ");
			Hql.append(" and id !='").append(t.getId()).append("'");
			List<User> list = userDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				List<User> list2 = userDao.queryHql("from User where 1=1 and id = "+t.getId());
				User user = list2.get(0);
				user.setUserName(userName);
				String PWD[] = t.getPassWord().split(",");
				user.setPassWord(PWD[0]);
				user.setIdRole(t.getIdRole());
				user.setRmk(user.getRmk());
				userDao.saveOrUpdate(user);
				/**添加日志*/
				logs.addLog(t, "修改 用户： "+user.getUserName()+" 成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}
	/**根据用户ID查找用户*/
	public List<User> findUser(String userId) {
		List<User> list = userDao.queryHql("from User where id = " + userId);
		return list;
	}
	/**查询条件查询用户信息*/
	public UserBean queryAll(UserBean entity, UserBean oldUserBean) {

		List userList = new ArrayList();
		if (entity.equals(oldUserBean)) {
			userList = oldUserBean.getAllList();
		} else {
			String name = entity.getUserName();
			String roleId = String.valueOf(entity.getIdRole());
			if (null == roleId || "0".equals(roleId)) {
				roleId = null;
			}
			StringBuffer sb = new StringBuffer();
			sb.append("from User where 1=1");
			if (StringUtils.isNotEmpty(name)) {
				sb.append(" and  userName like '%" + name.trim() + "%' ");
			}
			if (StringUtils.isNotEmpty(roleId)) {
				sb.append("  and idRole = '" + roleId + "'  ");
			}
				userList = userDao.queryHql(sb.toString());
		}
		if (CollectionUtils.isNotEmpty(userList)) {
			entity.setAllList(userList);
			entity.setList(userList.subList(entity.getCurrentRow(), entity.getLastRow()));
		}
			return entity;
	}

	/**查询所有用户供其他模块调用*/
	public List<User>  queryAllUser() {
		return userDao.queryAll();
	}
	
	/**
	 * 根据角色获取该角色的菜单
	 * @param role
	 * @return 菜单列表
	 */
	public List<Menu> getMenuByRole(Role role){
		String Hql = "from Menu where id in (" + role.getMenu() + ") order by id asc";

		return menuDao.queryHql(Hql);
	}

}
